package com.lovo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lovo.dto.BuyDTO;
import com.lovo.dto.SellDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Description:
 *
 * @author LiuChengQi
 * @date Created on 2020/11/30
 */
@RestController
public class BuyController {
    @Autowired
    private RestTemplate restTemplate;
    @Resource
    private ObjectMapper objectMapper;

    @GetMapping("buy")
    public Object buy(Integer id , Integer num){
        BuyDTO buyDTO = new BuyDTO(id, num);
        SellDTO sellDTO =
                restTemplate.postForEntity("http://127.0.0.1:8080/sell/",
                        buyDTO,SellDTO.class).getBody();
        String s = "";
        try {
             s = objectMapper.writeValueAsString(sellDTO);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return s;
    }

}
